package com.wonders.hisdoctor.station.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 换查询集合中的map的key,去掉下划线及下划线后首字母大写
 * @author tong-aj
 */
public class MapUtils {

    /**
     * 替换查询集合中的map的key,去掉下划线及下划线后首字母大写
     */
    public static List<Map<String, Object>> replaceMapKeyForList(List<Map<String, Object>> listMap) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Map<String, Object> map : listMap) {
            Map<String, Object> m = new HashMap<>();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                m.put(replaceUnderlineAndfirstToUpper(entry.getKey(), "_", ""), entry.getValue());
            }
            resultList.add(m);
        }
        return resultList;
    }

    /**
     * 替换查询集合中的map的key,去掉下划线及下划线后首字母大写
     */
    public static Map<String, Object> replaceMapKey(Map<String, Object> map) {
        Map<String, Object> result = new HashMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            result.put(replaceUnderlineAndfirstToUpper(entry.getKey(), "_", ""), entry.getValue());
        }
        return result;
    }

    /**
     * 替换字符串并让它的下一个字母为大写
     *
     * @param srcStr
     * @param org
     * @param ob
     * @return
     */
    public static String replaceUnderlineAndfirstToUpper(String srcStr, String org, String ob) {
        String newString = "";
        int first = 0;
        while (srcStr.indexOf(org) != -1) {
            first = srcStr.indexOf(org);
            if (first != srcStr.length()) {
                newString = newString + srcStr.substring(0, first) + ob;
                srcStr = srcStr.substring(first + org.length(), srcStr.length());
                srcStr = firstCharacterToUpper(srcStr);
            }
        }
        newString = newString + srcStr;
        return newString;
    }

    /**
     * 首字母大写
     *
     * @param srcStr
     * @return
     */
    public static String firstCharacterToUpper(String srcStr) {
        return srcStr.substring(0, 1).toUpperCase() + srcStr.substring(1);
    }

}
